/*
	Figure S2 Alternative favoritism codings
*/

use data_study_2.dta, clear


// Set omitted categories
	gen zero_race = 0
	label var zero_race "White"
	gen zero_sex = 0
	label var zero_sex "Male"
	gen zero_eco = 0
	label var zero_eco "Maintain investment in energy"
	gen zero_biden = 0
	label var zero_biden "Vote Share: 51%"
	gen zero_exp = 0
	label var zero_exp "Political newcomer"
	gen zero_dist = 0
	label var zero_dist "[63, 8, 13, 11, 5]"
	gen zero_racepol = 0
	label var zero_racepol "Not shown policy"
	
// Set regression variables
	loc reg_race "cand_black zero_race cand_asian cand_hispa"
	loc reg_sex "cand_female zero_sex"
	loc reg_exp "cand_exp_teach cand_exp_council cand_exp_lawyer cand_exp_business zero_exp"
	loc reg_biden "cand_biden_p59 cand_biden_p57 cand_biden_p55 cand_biden_p53 zero_biden"
	loc reg_distpop "cand_dist1 cand_dist2 cand_dist3 cand_dist4 cand_dist5 cand_dist6 zero_dist"
	loc reg_issues "cand_policy_abort1 cand_policy_abort2 cand_policy_tax1 cand_policy_tax2"
	loc reg_issues "`reg_issues' cand_policy_health1 cand_policy_health2 cand_policy_eco1 zero_eco"
	loc reg_affirm "cand_policy_aa1 cand_policy_aa2 cand_policy_aa3 zero_racepol"
	loc reg_affirm2 "noracepolicy cand_policy_aa1 cand_policy_aa2 cand_policy_aa3 "
	
// Interactive Terms for Black Candidate X Affirmative Action
	gen noracepolicy = cand_policy_aa1 == 0 & cand_policy_aa2 == 0 & cand_policy_aa3 == 0	
	
	loc int ""
	foreach r in white black asian hispa {
		gen `r'_aa1 = cand_policy_aa1*cand_`r'
		gen `r'_aa2 = cand_policy_aa2*cand_`r'
		gen `r'_aa3 = cand_policy_aa3*cand_`r'
		gen `r'_aa0 = noracepolicy*cand_`r'
		
		loc lab = proper("`r'")
		if "`lab'" == "Hispa" loc lab = "Hispanic"
	
		label var `r'_aa0 "`lab' X No Position"
		label var `r'_aa1 "`lab' X Expand"
		label var `r'_aa2 "`lab' X Keep"
		label var `r'_aa3 "`lab' X End"
		
		loc int "`int' `r'_aa0 `r'_aa1 `r'_aa2 `r'_aa3"
	}
	
	
	// set reference
	replace white_aa0 = 0
	label var noracepolicy "Not shown position"
	
	// Favoritism measures
	gen out_fair_bwdiff = out_fair_black - out_fair_white
	gen favor_black = out_fair_bwdiff > 0
	gen favor_white = out_fair_bwdiff < 0
	
//============================================================================== Regress the things
	// All attributes, pooled 
		eststo clear
		reg out_fair_bwdiff `reg_race' `reg_sex' cand_age `reg_exp' `reg_biden' `reg_distpop' `reg_issues' `reg_affirm', vce(cluster r_id)
		eststo pool_fair
		loc b_pool_fair = string(round(_b[_cons], 0.001), "%9.3f")
	
		reg favor_black `reg_race' `reg_sex' cand_age `reg_exp' `reg_biden' `reg_distpop' `reg_issues' `reg_affirm', vce(cluster r_id)
		eststo pool_favorb
		loc b_pool_favorb = string(round(_b[_cons], 0.001), "%9.3f")
		
		reg favor_white `reg_race' `reg_sex' cand_age `reg_exp' `reg_biden' `reg_distpop' `reg_issues' `reg_affirm', vce(cluster r_id)
		eststo pool_favorw
		loc b_pool_favorw = string(round(_b[_cons], 0.001), "%9.3f")
		
		coefplot (pool_fair, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
					, bylabel("{bf:(a) Black over White, Differ.}") || ///
				(pool_favorb, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
					, bylabel("{bf:(b) Black over White, Binary}") || ///	
				 (pool_favorw, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
					, bylabel("{bf:(c) White over Black, Binary}") ///
				|| , drop(_cons cand_age) omitted baselevels ms(c) msize(medsmall) ///
				ylabel(,labsize(vsmall)) ///
			xline(0, lc(black)) nokey ///
			subtitle(, bcolor(white) color(black) size(vsmall)) ///
			byopts(row(1) note("`notes'", size(vsmall)) t1title("{bf:`title'}", size(small))) ///
			xtitle("Effects of Candidate Attributes (Scale 0 to 1)", size(vsmall)) ///
			xlabel(-0.2(0.1)0.2,labsize(small)) norecycle ///
			headings(cand_black = "{bf: Race}" ///
					cand_female = "{bf: Gender}" ///
					cand_exp_teach = "{bf: Occupation}" ///
					cand_biden_p59 = "{bf: District Vote for Biden}" ///
					cand_dist1 = "{bf: District Racial % [W,B,A,H,O]}" ///
					cand_policy_abort1 = "{bf: Abortion}" ///
					cand_policy_tax1 = "{bf: Tax Policy}" ///
					cand_policy_health1 = "{bf: Healthcare}" ///
					cand_policy_eco1 = "{bf: Energy}" ///
					cand_policy_aa1 = "{bf: Affirmative Action}" ///
					, labsize(vsmall)) 
	
		addplot 1: ,note("Mean, Baseline Categories = `b_pool_fair'", size(vsmall)) norescaling
		addplot 2: ,note("Mean, Baseline Categories = `b_pool_favorb'", size(vsmall)) norescaling
		addplot 3: ,note("Mean, Baseline Categories = `b_pool_favorw'", size(vsmall)) norescaling
		
		graph display, xsize(5) ysize(3.7) margins(vsmall)	
		// Pooled outcomes (not in paper)
		
	// Effect of candidate attributes on main outcomes, interacted
		// Interacted
		reg out_fair_bwdiff `reg_sex' cand_age `reg_exp' `reg_biden' `reg_distpop' `reg_issues' `int' cand_age , vce(cluster r_id)
		eststo fair_bwdiff
		loc b_fair = string(round(_b[_cons], 0.001), "%9.3f")
		
		reg favor_black `reg_sex' cand_age `reg_exp' `reg_biden' `reg_distpop' `reg_issues' `int' cand_age , vce(cluster r_id)
		eststo pool_favorb
		loc b_pool_favorb = string(round(_b[_cons], 0.001), "%9.3f")
	
		reg favor_white `reg_sex' cand_age `reg_exp' `reg_biden' `reg_distpop' `reg_issues' `int' cand_age , vce(cluster r_id)
		eststo pool_favorw
		loc b_pool_favorw = string(round(_b[_cons], 0.001), "%9.3f")
	
		coefplot (fair_bwdiff, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
					, bylabel("{bf:(a) Black over White, Differ.}") || ///
				(pool_favorb, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
					, bylabel("{bf:(b) Black over White, Binary}") || ///		
				 (pool_favorw, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
					, bylabel("{bf:(c) White over Black, Binary}") ///
				|| , drop(_cons `reg_sex' `reg_issues' cand_age `reg_exp' `reg_biden' `reg_distpop') ///
				omitted baselevels ms(c) msize(med) ///
				ylabel(,labsize(vsmall)) ///
			xline(0, lc(black)) nokey ///
			subtitle(, bcolor(white) color(black) size(vsmall)) ///
			byopts(row(1) t1title("{bf:`title'}", size(small))) ///
			xtitle("Effects of Candidate Attributes (Scale 0 to 1)", size(vsmall)) ///
			xlabel(-0.2(0.1)0.2,labsize(small)) norecycle ///
			headings(white_aa0 = "{bf: White X Affirmative Action}" ///
					black_aa0 = "{bf: Black X Affirmative Action}" ///
					asian_aa0 = "{bf: Asian X Affirmative Action}" ///
					hispa_aa0 = "{bf: Hispanic X Affirmative Action}" ///
					, labsize(vsmall)) 

		addplot 1: ,note("Mean, Baseline Categories = `b_fair'", size(vsmall)) norescaling
		addplot 2: ,note("Mean, Baseline Categories = `b_pool_favorb'", size(vsmall)) norescaling
		addplot 3: ,note("Mean, Baseline Categories = `b_pool_favorw'", size(vsmall)) norescaling
		
	
		graph display, xsize(4.5) ysize(3.4) margins(vsmall)
		
		graph export figure_s2.png, as(png) replace
	
